/**
 * 地址字段适配器
 * 将后端返回的字段映射为前端期望的字段格式
 */

/**
 * 适配单个地址数据
 * @param {Object} rawAddress - 后端返回的原始地址数据
 * @returns {Object} - 适配后的地址数据
 */
export function adaptAddressData(rawAddress) {
  if (!rawAddress) return null
  
  // 创建适配后的地址对象
  const adaptedAddress = {
    id: rawAddress.id || rawAddress.addressId,
    receiver_name: rawAddress.receiverName || rawAddress.receiver_name || rawAddress.name || '',
    receiver_phone: rawAddress.receiverPhone || rawAddress.receiver_phone || rawAddress.phone || rawAddress.mobile || '',
    province: rawAddress.province || '',
    city: rawAddress.city || '',
    district: rawAddress.district || rawAddress.area || '',
    street: rawAddress.street || rawAddress.address || rawAddress.detailAddress || '',
    is_default: rawAddress.isDefault || rawAddress.is_default || rawAddress.defaultFlag || false,
    user_id: rawAddress.userId || rawAddress.user_id || rawAddress.userId || '',
    // 保留原始字段，以防需要
    _raw: rawAddress
  }
  
  // 处理布尔值转换
  if (typeof adaptedAddress.is_default === 'string') {
    adaptedAddress.is_default = adaptedAddress.is_default === '1' || adaptedAddress.is_default === 'true'
  }
  
  // 处理数字转换
  if (typeof adaptedAddress.id === 'string') {
    adaptedAddress.id = parseInt(adaptedAddress.id) || 0
  }
  if (typeof adaptedAddress.user_id === 'string') {
    adaptedAddress.user_id = parseInt(adaptedAddress.user_id) || 0
  }
  
  return adaptedAddress
}

/**
 * 适配地址列表数据
 * @param {Array} rawAddressList - 后端返回的原始地址列表
 * @returns {Array} - 适配后的地址列表
 */
export function adaptAddressList(rawAddressList) {
  if (!Array.isArray(rawAddressList)) {
    console.warn('⚠️ 地址列表不是数组格式:', rawAddressList)
    return []
  }
  
  return rawAddressList.map((address, index) => {
    const adapted = adaptAddressData(address)
    if (!adapted) {
      console.warn(`⚠️ 地址 ${index} 适配失败:`, address)
    }
    return adapted
  }).filter(Boolean)
}

/**
 * 检查字段映射是否成功
 * @param {Object} adaptedAddress - 适配后的地址数据
 * @returns {Object} - 检查结果
 */
export function checkFieldMapping(adaptedAddress) {
  const requiredFields = ['receiver_name', 'receiver_phone', 'province', 'city', 'district', 'street']
  const missingFields = requiredFields.filter(field => !adaptedAddress[field])
  
  return {
    success: missingFields.length === 0,
    missingFields,
    adaptedAddress
  }
}

/**
 * 调试：打印字段映射信息
 * @param {Object} rawAddress - 原始数据
 * @param {Object} adaptedAddress - 适配后数据
 */
export function debugFieldMapping(rawAddress, adaptedAddress) {
  console.log('🔍 字段映射调试:')
  console.log('原始数据字段:', Object.keys(rawAddress))
  console.log('适配后字段:', Object.keys(adaptedAddress))
  console.log('映射结果:', {
    'receiver_name': `${rawAddress.receiverName || rawAddress.receiver_name || rawAddress.name || 'undefined'} -> ${adaptedAddress.receiver_name}`,
    'receiver_phone': `${rawAddress.receiverPhone || rawAddress.receiver_phone || rawAddress.phone || rawAddress.mobile || 'undefined'} -> ${adaptedAddress.receiver_phone}`,
    'is_default': `${rawAddress.isDefault || rawAddress.is_default || rawAddress.defaultFlag || 'undefined'} -> ${adaptedAddress.is_default}`,
    'user_id': `${rawAddress.userId || rawAddress.user_id || 'undefined'} -> ${adaptedAddress.user_id}`
  })
} 